﻿using System;
using System.Drawing;

namespace Spencen.Mobile.UI.Transitions
{
    public class SlideTransition : Transition
    {
        public Point Offset { get; set; }

        protected override void RenderFrame( Graphics compositeGraphics, Bitmap compositeBitmap, Bitmap newViewBitmap, Bitmap oldViewBitmap, Point startOffset, Brush backBrush, float adjustedValue )
        {
            Point currentOffset = new Point( startOffset.X + (int)( Offset.X * adjustedValue ), startOffset.Y + (int)( Offset.Y * adjustedValue ) );

            var width = NewView.Size.Width;
            var height = NewView.Size.Height;

            // Paint the "overrun" that may occur when using certain types of easing - e.g. elastic, bounce.
            if ( Offset.X < 0 && currentOffset.X < Offset.X )
                compositeGraphics.FillRectangle( backBrush,
                    currentOffset.X + width + width, 0,
                    -currentOffset.X - width, height );
            else if ( Offset.X > 0 && currentOffset.X > Offset.X )
                compositeGraphics.FillRectangle( backBrush,
                    0, 0,
                    currentOffset.X - width, height );

            // Place the old view on the composite at the new offset position.
            compositeGraphics.DrawImage( oldViewBitmap, currentOffset.X, currentOffset.Y );
            // Place the new view on the composite either to the left or right of the old view.
            compositeGraphics.DrawImage( newViewBitmap,
                Offset.X < 0 ? currentOffset.X + width : currentOffset.X - width,
                currentOffset.Y );
        }
    }
}
